package simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import config.RabbitMqConnection;
import org.junit.Test;

/**
 * @description: 消息发布者（提供者）,简单直连模式，最简单的模式，一个 provider 对应一个 queue 和一个 consumer
 * @author:刘腾辉
 * @create:2020/10/20 9:38
 */
public class Provider {
    @Test
    public void Publish() throws Exception {
        String msg = "Hello world";

        /**
         * 1.获取连接
         */
        Connection rabbitMqConnection = RabbitMqConnection.getRabbitMqConnection();

        /**
         * 2.创建 channel
         */
        Channel channel = rabbitMqConnection.createChannel();

        /**
         * 3.发布消息到 exchange
         */
        channel.basicPublish("","HelloWorld",null,msg.getBytes());

        /**
         * 4.关闭资源
         */
        System.out.println("生产者消息发布完毕!");
        channel.close();
        rabbitMqConnection.close();
    }
}
